<template>
  <div class="login-wrap">
    <h3>后台管理系统</h3>
    <el-form :model="ruleForm" labelWidth="80px" ref="ruleForm" :rules="rules">
      <el-form-item label="用户名" prop="name" required >
        <el-input v-model="ruleForm.name" placeholder="请输入用户姓名"></el-input>

      </el-form-item>
      <el-form-item label="密码" prop="password" required>
        <el-input type="password" v-model="ruleForm.password" placeholder="请输入用户姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登入</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>

  export default {
    data: function () {
      return {
        hello: 'hello world',
        ruleForm:{
          name:'',
          password:''
        },
        rules:{
          name:[
            {required:true,message:'请输入用户名',trigger:'blur'}
          ],
          password:[
            {required:true,message:'请输入密码',trigger:'blur'}
          ]
        }
      }
    },
    methods:{
      submitForm(formName) {
        const self = this;
        self.$refs[formName].validate((valid)=>{
          if(valid){
            self.$store.commit('setUserName',{name:self.ruleForm.name})
            self.$router.push('/home')
          }else{
            console.log('error submit')
            return false;
          }
        });
      },
      resetForm(formName) {
        console.log("reset...")
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped>
.login-wrap{
  width: 200px;
  height: 300px;
  margin: 100px auto;
}
</style>

